var crypto = require('../utils/crypto');
var express = require('express');
var db = require('../db');

var app = express();

//设置跨域访问
app.all('*', function(req, res, next) {
	res.header("Access-Control-Allow-Origin", "*");
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
	res.header("X-Powered-By",' 3.2.1');
	res.header("Content-Type", "application/json;charset=utf-8");
	next();
});

app.get('/rigister_gs', function(req, res) {
	var ip = req.ip;
	var clientip = req.query.clientip;
	var clientport = req.query.clientport;
	var httpport = req.query.httpport;
	var load = req.query.load;
	var id = clientip + ":" + clientport;
})



function chooseServer() {
	var serverinfo = null;
	for (var s in serverMap) {
		var info = serverMap[s];
		if (serverinfo == null) {
			serverinfo = info;
		} else {
			if(serverinfo.load > info.load) {
				serverinfo = info;
			}
		}
	}
	return serverinfo;
}

exports.createRoom = function(account, userId, roomConf, fnCallback) {
	var serverInfo = chooseServer();
	if(serverInfo == null) {
		fnCallback(101, null);
		return ;
	}
	db.get_gems(account, function(data) {
		if (data != null) {
			var reqdata = {
				userId: userId,
				gems: data.gems,
				conf: roomConf
			};
			reqdata.sign = crypto.md5(userId + roomConf + data.gems + config.ROOM_PRI_KEY);
			http.get(serverinfo.ip, serverinfo.httpport, '/create_room', function(ret, data) {
				if(ret) {
					if(data.errcode == 0) {
						fnCallback(0, data.roomId);
					} else {
						fnCallback(data.errcode);
					}
					return;
				}
				fnCallback(102);
			})
		} else {
			fnCallback(103);
		}
	});
}

exports.enterRoom = function(userId, name, roomId, fnCallback) {
	var reqdata = {
		userId: userId,
		name: name,
		roomId = roomId
	};
	reqdata.sign = crypto.md5(userId + name + roomId + config.ROOM_PRI_KEY);
	var checkRoomIsRunning = function(serverinfo, roomId, callback) {
		var sign = crypto.md5(roomId + config.ROOM_PRI_KEY);
		http.get(serverinfo.ip,serverinfo.httpport,'/is_room_runing', {roomId:roomId,sign:sign}, function(ret, data) {
			if(ret) {
				callback((data.errcode == 0 && data.runing == true));
			} else {
				callback(false);
			}
		})
	}
	var enterRoomReq = function(serverinfo) {
		http.get(serverinfo.ip,serverinfo.httpport,'/enter_room',reqdata,function(req, data) {
			if(ret) {
				if(data.errcdoe == 0) {
					db.set_room_id_of_user(userId,roomId,function(ret) {
						fnCallback(0, {
							ip:serverinfo.clientip,
							port:serverinfo.clientport,
							token:data.token
						});
					});
				} else {
					fnCallback(data.errcode);
				}
			} else {
				fnCallback(-1);
			}
		})
	}
	var chooseServerAndEnter = function(serverinfo) {
		serverinfo = chooseServer();
		if(serverinfo != null) {
			enterRoomReq(serverinfo);
		} else {
			fnCallback(-1);
		}
	}
	db.get_room_addr(roomId, function(ret, ip, port) {
		if (ret) {
			var id = ip + ':' + port;
			var serverinfo = serverMap[id];
			if(serverinfo != null) {
				//存在房间地址
				checkRoomIsRunning(serverinfo, roomId, function(ret) {
					if(ret) {
						enterRoomReq(serverInfo);
					} else {
						chooseServerAndEnter(serverinfo);
					}
				});
			} else {
				chooseServerAndEnter(serverinfo);
			}
		} else {
			fnCallback(-2);
		}
	});
}